package com.moxiao.yfjh.module.blind.service.memberSayHello;

import com.moxiao.yfjh.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.moxiao.yfjh.module.blind.controller.app.controller.exchage.vo.AppMemberSayhelloReqVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.moxiao.yfjh.module.blind.controller.admin.memberSayHello.vo.MemberSayhelloCreateReqVO;
import com.moxiao.yfjh.module.blind.controller.admin.memberSayHello.vo.MemberSayhelloExportReqVO;
import com.moxiao.yfjh.module.blind.controller.admin.memberSayHello.vo.MemberSayhelloPageReqVO;
import com.moxiao.yfjh.module.blind.controller.admin.memberSayHello.vo.MemberSayhelloUpdateReqVO;
import com.moxiao.yfjh.module.blind.dal.dataobject.memberLike.MemberLikeDO;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import com.moxiao.yfjh.module.blind.controller.admin.memberSayHello.vo.*;
import com.moxiao.yfjh.module.blind.dal.dataobject.memberSayHello.MemberSayhelloDO;
import com.moxiao.yfjh.framework.common.pojo.PageResult;

import com.moxiao.yfjh.module.blind.convert.memberSayHello.MemberSayhelloConvert;
import com.moxiao.yfjh.module.blind.dal.mysql.memberSayHello.MemberSayhelloMapper;

import static com.moxiao.yfjh.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.moxiao.yfjh.module.blind.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 相亲会员打招呼 Service 实现类
 *
 * @author mx
 */
@Service
@Validated
public class MemberSayhelloServiceImpl implements MemberSayhelloService {

    @Resource
    private MemberSayhelloMapper memberSayhelloMapper;

    @Override
    public Long createMemberSayhello(MemberSayhelloCreateReqVO createReqVO) {
        // 插入
        MemberSayhelloDO memberSayhello = MemberSayhelloConvert.INSTANCE.convert(createReqVO);
        memberSayhelloMapper.insert(memberSayhello);
        // 返回
        return memberSayhello.getId();
    }

    @Override
    public void updateMemberSayhello(MemberSayhelloUpdateReqVO updateReqVO) {
        // 校验存在
        validateMemberSayhelloExists(updateReqVO.getId());
        // 更新
        MemberSayhelloDO updateObj = MemberSayhelloConvert.INSTANCE.convert(updateReqVO);
        memberSayhelloMapper.updateById(updateObj);
    }

    @Override
    public void deleteMemberSayhello(Long id) {
        // 校验存在
        validateMemberSayhelloExists(id);
        // 删除
        memberSayhelloMapper.deleteById(id);
    }

    private void validateMemberSayhelloExists(Long id) {
        if (memberSayhelloMapper.selectById(id) == null) {
            throw exception(MEMBER_SAYHELLO_NOT_EXISTS);
        }
    }

    @Override
    public MemberSayhelloDO getMemberSayhello(Long id) {
        return memberSayhelloMapper.selectById(id);
    }

    @Override
    public List<MemberSayhelloDO> getMemberSayhelloList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return memberSayhelloMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<MemberSayhelloDO> getMemberSayhelloPage(MemberSayhelloPageReqVO pageReqVO) {
        return memberSayhelloMapper.selectPage(pageReqVO);
    }


    @Override
    public List<MemberSayhelloDO> getMemberSayhelloList(MemberSayhelloExportReqVO exportReqVO) {
        return memberSayhelloMapper.selectList(exportReqVO);
    }

    @Override
    public List<MemberSayhelloDO> getMemberSayhelloList(AppMemberSayhelloReqVO appMemberSayhelloReqVO) {
        return memberSayhelloMapper.selectSayHelloList(appMemberSayhelloReqVO);
    }

    @Override
    public MemberSayhelloDO getBlindMemberSayHello(AppMemberSayhelloReqVO appMemberSayhelloReqVO) {
        LambdaQueryWrapperX<MemberSayhelloDO> memberSayHelloDOWrapperX = new LambdaQueryWrapperX<>();
        memberSayHelloDOWrapperX.and(wrapper->wrapper.eq(MemberSayhelloDO::getReceive, appMemberSayhelloReqVO.getReceive()).eq(MemberSayhelloDO::getIssue, appMemberSayhelloReqVO.getIssue()))
                .or(wrapper->wrapper.eq(MemberSayhelloDO::getReceive, appMemberSayhelloReqVO.getIssue()).eq(MemberSayhelloDO::getIssue, appMemberSayhelloReqVO.getReceive()));
        return memberSayhelloMapper.selectOne(memberSayHelloDOWrapperX);
    }

    @Override
    public Long greeMeNum(Long userId) {
        LambdaQueryWrapper<MemberSayhelloDO> lqw = Wrappers.lambdaQuery();
        lqw.and(wrapper->wrapper.eq(MemberSayhelloDO::getReceive, userId))
                .or(wrapper->wrapper.eq(MemberSayhelloDO::getIssue, userId).eq(MemberSayhelloDO::getSayHello, (byte) 1));
        return memberSayhelloMapper.selectCount(lqw);
    }

}
